/*! The MIT License (MIT) Copyright (c) 2015 Senthil Porunan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Source: https://github.com/senthilporunan/jRate Demo: http://www.toolitup.com/JRate.html **/ ; (function($, undefined) { $.fn.jRate = function(options) { "use strict"; var $jRate = $(this); var defaults = { rating: 0, shape: "STAR", count: 5, width: "20", height: "20", widthGrowth: 0.0, heightGrowth: 0.0, backgroundColor: "white", startColor: "yellow", endColor: "green", strokeColor: "black", transparency: 1, shapeGap: "0px", opacity: 1, min: 0, max: 5, precision: 0.1, minSelected: 0, strokeWidth: '2px', horizontal: true, reverse: false, readOnly: false, touch: true, onChange: null, onSet: null }; var settings = $.extend({}, defaults, options); var startColorCoords, endColorCoords, shapes; function isDefined(name) { return typeof name !== "undefined"; } function getRating() { if (isDefined(settings)) return settings.rating; } function setRating(rating) { if (!isDefined(rating) || rating < settings.min || rating > settings.max) throw rating + " is not in range(" + min + "," + max + ")"; settings.rating = rating; showRating(rating); } function getShape(currValue) { var header = '' + '' + '' + ''; var shapeRate; switch (settings['shape']) { case 'STAR': shapeRate = header + 'viewBox="0 12.705 512 486.59"' + '>' + linearGrad + '' + ''; break; case 'CIRCLE': shapeRate = header + '>' + linearGrad + '' + ''; break; case 'RECTANGLE': shapeRate = header + '>' + linearGrad + '' + ''; break; case 'TRIANGLE': shapeRate = header + '>' + linearGrad + '' + ''; break; case 'RHOMBUS': shapeRate = header + '>' + linearGrad + '' + ''; break; case 'FOOD': shapeRate = header + 'viewBox="0 0 50 50"' + '>' + linearGrad + ''+''; break; case 'TWITTER': shapeRate = header + 'viewBox="0 0 512 512"' + '>' + linearGrad + ''+''; break; case 'BULB': shapeRate = header + 'viewBox="0 0 512 512"' + '>' + linearGrad + ''+''; break; case 'BASKET': shapeRate = header + 'viewBox="0 0 30 30"' + '>' + linearGrad + ''+''; break; default: throw Error("No such shape as " + settings['shape']); } return shapeRate; } function setCSS() { // setup css properies $jRate.css("white-space", "nowrap"); $jRate.css("cursor", "pointer"); $jRate.css('fill', settings['shape']); } function bindEvents($svg, i) { $svg.on("mousemove", onMouseEnter(i)) .on("mouseenter", onMouseEnter(i)) .on("click", onMouseClick(i)) .on("mouseover", onMouseEnter(i)) .on("hover", onMouseEnter(i)) .on("mouseleave", onMouseLeave) .on("mouseout", onMouseLeave) .on("JRate.change", onChange) .on("JRate.set", onSet); if (settings.touch) { $svg.on("touchstart", onTouchEnter(i)) .on("touchmove", onTouchEnter(i)) .on("touchend", onTouchClick(i)) .on("tap", onTouchClick(i)); } } function showNormalRating() { var id = $jRate.attr('id'); for (var i = 0; i < settings.count; i++) { shapes.eq(i).find('#'+id+'_grad'+(i+1)).find("stop").eq(0).attr({ 'offset': '0%' }); shapes.eq(i).find('#'+id+'_grad'+(i+1)).find("stop").eq(0).attr({ 'stop-color': settings.backgroundColor }); shapes.eq(i).find('#'+id+'_grad'+(i+1)).find("stop").eq(1).attr({ 'offset': '0%' }); shapes.eq(i).find('#'+id+'_grad'+(i+1)).find("stop").eq(1).attr({ 'stop-color': settings.backgroundColor }); } } function showRating(rating) { showNormalRating(); var singleValue = (settings.max - settings.min) / settings.count; rating = (rating - settings.min) / singleValue; var fillColor = settings.startColor; var id = $jRate.attr('id'); if (settings.reverse) { for (var i = 0; i < rating; i++) { var j = settings.count - 1 - i; shapes.eq(j).find('#'+id+'_grad'+(j+1)).find("stop").eq(0).attr({ 'offset': '100%' }); shapes.eq(j).find('#'+id+'_grad'+(j+1)).find("stop").eq(0).attr({ 'stop-color': fillColor }); if (parseInt(rating) !== rating) { var k = Math.ceil(settings.count - rating) - 1; shapes.eq(k).find('#'+id+'_grad'+(k+1)).find("stop").eq(0).attr({ 'offset': 100 - (rating * 10 % 10) * 10 + '%' }); shapes.eq(k).find('#'+id+'_grad'+(k+1)).find("stop").eq(0).attr({ 'stop-color': settings.backgroundColor }); shapes.eq(k).find('#'+id+'_grad'+(k+1)).find("stop").eq(1).attr({ 'offset': 100 - (rating * 10 % 10) * 10 + '%' }); shapes.eq(k).find('#'+id+'_grad'+(k+1)).find("stop").eq(1).attr({ 'stop-color': fillColor }); } if (isDefined(endColorCoords)) { fillColor = formulateNewColor(settings.count - 1, i); } } } else { for (var i = 0; i < rating; i++) { shapes.eq(i).find('#'+id+'_grad'+(i+1)).find("stop").eq(0).attr({ 'offset': '100%' }); shapes.eq(i).find('#'+id+'_grad'+(i+1)).find("stop").eq(0).attr({ 'stop-color': fillColor }); if (rating * 10 % 10 > 0) { shapes.eq(Math.ceil(rating) - 1).find('#'+id+'_grad'+(i+1)).find("stop").eq(0).attr({ 'offset': (rating * 10 % 10) * 10 + '%' }); shapes.eq(Math.ceil(rating) - 1).find('#'+id+'_grad'+(i+1)).find("stop").eq(0).attr({ 'stop-color': fillColor }); } if (isDefined(endColorCoords)) { fillColor = formulateNewColor(settings.count, i); } } } } var formulateNewColor = function(totalCount, currentVal) { var avgFill = []; for (var i = 0; i < 3; i++) { var diff = Math.round((startColorCoords[i] - endColorCoords[i]) / totalCount); var newValue = startColorCoords[i] + (diff * (currentVal + 1)); if (newValue / 256) avgFill[i] = (startColorCoords[i] - (diff * (currentVal + 1))) % 256; else avgFill[i] = (startColorCoords[i] + (diff * (currentVal + 1))) % 256; } return "rgba(" + avgFill[0] + "," + avgFill[1] + "," + avgFill[2] + "," + settings.opacity + ")"; }; function colorToRGBA(color) { var cvs, ctx; cvs = document.createElement('canvas'); cvs.height = 1; cvs.width = 1; ctx = cvs.getContext('2d'); ctx.fillStyle = color; ctx.fillRect(0, 0, 1, 1); return ctx.getImageData(0, 0, 1, 1).data; } function onMouseLeave() { if (!settings.readOnly) { showRating(settings.rating); onChange(null, {rating : settings.rating}); } } function workOutPrecision(num) { var multiplactiveInverse = 1/settings.precision; return Math.ceil(num*multiplactiveInverse)/multiplactiveInverse; } function onEnterOrClickEvent(e, ith, label, update) { if (settings.readOnly) return; var svg = shapes.eq(ith - 1); var partial; if (settings.horizontal) { partial = (e.pageX - svg.offset().left) / svg.width(); } else { partial = (e.pageY - svg.offset().top) / svg.height(); } var count = (settings.max - settings.min) / settings.count; partial = (settings.reverse) ? partial : 1 - partial; var rating = ((settings.reverse ? (settings.max - settings.min - ith + 1) : ith) - partial) * count; rating = settings.min + Number(workOutPrecision(rating)); if (rating < settings.minSelected) rating = settings.minSelected; if (rating <= settings.max && rating >= settings.min) { showRating(rating); if (update) settings.rating = rating; svg.trigger(label, { rating: rating }); } } function onTouchOrTapEvent(e, ith, label, update) { if (settings.readOnly) return; var touches = e.originalEvent.changedTouches; // Ignore multi-touch if (touches.length > 1) return; var touch = touches[0]; var svg = shapes.eq(ith - 1); var partial; if (settings.horizontal) { partial = (touch.pageX - svg.offset().left) / svg.width(); } else { partial = (touch.pageY - svg.offset().top) / svg.height(); } var count = (settings.max - settings.min) / settings.count; partial = (settings.reverse) ? partial : 1 - partial; var rating = ((settings.reverse ? (settings.max - settings.min - ith + 1) : ith) - partial) * count; rating = settings.min + Number(workOutPrecision(rating)); if (rating < settings.minSelected) rating = settings.minSelected; if (rating <= settings.max && rating >= settings.min) { showRating(rating); if (update) settings.rating = rating; svg.trigger(label, { rating: rating }); } } function onMouseEnter(i) { return function(e) { onEnterOrClickEvent(e, i, "JRate.change"); }; } function onMouseClick(i) { return function(e) { onEnterOrClickEvent(e, i, "JRate.set", true); }; } function onTouchEnter(i) { return function(e) { onTouchOrTapEvent(e, i, "JRate.touch"); }; } function onTouchClick(i) { return function(e) { onTouchOrTapEvent(e, i, "JRate.tap", true); }; } function onChange(e, data) { if (settings.onChange && typeof settings.onChange === "function") { settings.onChange.apply(this, [data.rating]); } } function onSet(e, data) { if (settings.onSet && typeof settings.onSet === "function") { settings.onSet.apply(this, [data.rating]); } } function drawShape() { var svg, i, sw, sh; for (i = 0; i < settings.count; i++) { $jRate.append(getShape(i+1)); } shapes = $jRate.find('svg'); for (i = 0; i < settings.count; i++) { svg = shapes.eq(i); bindEvents(svg, i + 1); if (!settings.horizontal) { svg.css({ 'display': 'block', 'margin-bottom': settings.shapeGap || '0px' }); } else { svg.css('margin-right', (settings.shapeGap || '0px')); } if (settings.widthGrowth) { sw = 'scaleX(' + (1 + settings.widthGrowth * i) + ')'; svg.css({ 'transform': sw, '-webkit-transform': sw, '-moz-transform': sw, '-ms-transform': sw, '-o-transform': sw, }); } if (settings.heightGrowth) { sh = 'scaleY(' + (1 + settings.heightGrowth * i) + ')'; svg.css({ 'transform': sh, '-webkit-transform': sh, '-moz-transform': sh, '-ms-transform': sh, '-o-transform': sh, }); } } showNormalRating(); showRating(settings.rating); shapes.attr({ width: settings.width, height: settings.height }); } //TODO //Validation implementation //Mini to max size //TODO Add this as a part of validation if (settings.startColor) startColorCoords = colorToRGBA(settings.startColor); if (settings.endColor) endColorCoords = colorToRGBA(settings.endColor); setCSS(); drawShape();; return $.extend({}, this, { "getRating": getRating, "setRating": setRating, "setReadOnly": function(flag) { settings.readOnly = flag; }, "isReadOnly": function() { return settings.readOnly; }, }); }; }(jQuery));